﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXDBC
    {
        public void MapQVW_MBST(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveQVW_MBST(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet Result = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            Result = QueryQVW_MBST(dwData);
            if (Result.Tables.Count > 0)
            {
                tbLocal = Result.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }

            MapQVW_MBST(ref dwLocal, dwData);

            if (!flgExist)
            {
                InsertQVW_MBST(dwData, 1);
            }
            UpdateQVW_MBST(dwLocal);

        }
        public void InsertQVW_MBST(DataRow dwData, int option = 1)
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1://20140501
                    or1.CommandText = "INSERT INTO QVW_MBST  "
                        + " SELECT DISTINCT TO_CHAR(SYSDATE,'YYYYMMDD') AS SVDATE,'M' AS ARTST, MBMAT AS PART,MBVAR AS PVAR,'          ' AS PCOLOR,'          ' AS PDESS,'          ' AS PDCWY,'          ' AS PFINISH,'       ' AS PSIZE,MBLO AS LO,MBLB AS LB,0.0 AS MSTQTY,0.0 AS MOVEIN,0.0 AS MOVEOUT,0.0 AS MADJUST,SUM(MBME*0.001) AS STWEIGHT"
                        + " FROM MBST WHERE MBMANDANT=:MANDANT GROUP BY MBMANDANT,MBLO,MBLB,MBMAT,MBVAR ";
                    or1.Parameters.Add("MANDANT", OracleType.NVarChar).Value = dwData["MANDANT"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        }
        public void DeleteQVW_MBST(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM QVW_MBST X ";
                    break;
                case 2:
                    or1.CommandText = "DELETE FROM QVW_MBST X WHERE   SVDATE=TO_CHAR(SYSDATE,'YYYYMMDD')";
                    
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        }
        public DataSet QueryQVW_MBST(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "SELECT * FROM QVW_MBST";
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "QVW_MBST";
            Return = Result;
            return Return;
        }
        public void UpdateQVW_MBST(DataRow dwData, int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "UPDATE QVW_MBST B SET MOVEIN=(SELECT SUM(UQTY) FROM QVW_MCOST M WHERE M.BS IN ('01','10','11','21','13')  AND RTRIM(B.SVDATE)=RTRIM(M.WDATE) AND B.LO=M.LO AND B.LB=M.LB AND RTRIM(B.PART)=RTRIM(M.PART) AND RTRIM(B.PVAR)=RTRIM(M.PVAR) AND B.ARTST=M.ARTST) WHERE EXISTS (SELECT 1 FROM QVW_MCOST M WHERE  M.BS IN ('01','10','11','21','13') AND RTRIM(B.SVDATE)=RTRIM(M.WDATE) AND B.LO=M.LO AND B.LB=M.LB AND RTRIM(B.PART)=RTRIM(M.PART) AND RTRIM(B.PVAR)=RTRIM(M.PVAR) AND B.ARTST=M.ARTST)";
                    break;
                case 2:
                    or1.CommandText = "UPDATE QVW_MBST B SET MOVEOUT=(SELECT SUM(UQTY) FROM QVW_MCOST M WHERE M.BS IN ('12','20','22','23','26','58') AND RTRIM(B.SVDATE)=RTRIM(M.WDATE) AND B.LO=M.LO AND B.LB=M.LB AND  RTRIM(B.PART)=RTRIM(M.PART) AND RTRIM(B.PVAR)=RTRIM(M.PVAR) AND B.ARTST=M.ARTST) WHERE EXISTS (SELECT 1 FROM QVW_MCOST M WHERE  M.BS IN ('12','20','22','23','26','58') AND RTRIM(B.SVDATE)=RTRIM(M.WDATE) AND B.LO=M.LO AND B.LB=M.LB AND RTRIM(B.PART)=RTRIM(M.PART) AND RTRIM(B.PVAR)=RTRIM(M.PVAR) AND B.ARTST=M.ARTST) ";
                    break;
                case 3:
                    or1.CommandText = "UPDATE QVW_MBST B SET MADJUST=(SELECT SUM(UQTY) FROM QVW_MCOST M WHERE M.BS NOT IN ('01','10','11','21','13','12','20','22','23','26','58') AND RTRIM(B.SVDATE)=RTRIM(M.WDATE) AND B.LO=M.LO AND B.LB=M.LB  AND RTRIM(B.PART)=RTRIM(M.PART) AND RTRIM(B.PVAR)=RTRIM(M.PVAR) AND B.ARTST=M.ARTST) WHERE EXISTS (SELECT 1 FROM QVW_MCOST M WHERE  M.BS NOT IN ('01','10','11','21','13','12','20','22','23','26','58') AND RTRIM(B.SVDATE)=RTRIM(M.WDATE) AND B.LO=M.LO AND B.LB=M.LB AND  RTRIM(B.PART)=RTRIM(M.PART) AND RTRIM(B.PVAR)=RTRIM(M.PVAR) AND B.ARTST=M.ARTST)"; ;
                    break;
                                    case 4:
                      or1.CommandText = "UPDATE QVW_MBST SET MSTQTY=STWEIGHT-(MOVEIN-MOVEOUT-MADJUST) WHERE  STWEIGHT-(MOVEIN-MOVEOUT-MADJUST) >0";
                    break;
                    
 
            }
            Connect2Proxy(or1);
        }
    }
}
